import { SDKDataMod } from "./SDKDataMode";
import { SDKTools } from "./SDKTools";
import { WebDataProxy } from "./数据代理类/WebDataProxy";

export class Web {
    public static requireConfig(appId: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            let url = `https://fc-mp-cd413cab-acc3-418e-8712-cf0ad3f2dfc2.next.bspapp.com/router/client/game.pub_find_Game?game_appid=${appId}`;
            let xhr = new XMLHttpRequest();
            console.log("requireConfig start");
            xhr.open('GET', url, true);
            xhr.onreadystatechange = function () {
                console.log("xhr.responseText", xhr.responseText);
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    if (SDKTools.isAliPay) {
                        let dat: { code: number, msg: string, data: SDKDataMod.AdWebData } = xhr.responseText as any;
                        console.log("requireConfig", dat.data);
                        WebDataProxy.inst.SetAdWebData(dat.data);
                        return resolve(true)
                    }
                    let dat: { code: number, msg: string, data: SDKDataMod.AdWebData } = JSON.parse(xhr.responseText);
                    console.log("requireConfig", dat.data);
                    WebDataProxy.inst.SetAdWebData(dat.data);
                    return resolve(true)
                } else {
                    return resolve(false)
                }
            };
            xhr.send();
        })
    }

    public static requireOpenID(appId: string, code: any): Promise<boolean> {
        if (WebDataProxy.inst.OpenIDData.m_OpenID) {
            this.__requireOpenID(appId, code);
            return Promise.resolve(true);
        }
        return this.__requireOpenID(appId, code);

    }

    private static __requireOpenID(appId: string, code: any): Promise<boolean> {
        return new Promise((resolve, reject) => {
            let url = `https://fc-mp-cd413cab-acc3-418e-8712-cf0ad3f2dfc2.next.bspapp.com/router/client/game.pub_get_openid?game_appid=${appId}&code=${code}`;
            let xhr = new XMLHttpRequest();
            // {"error_msg":"Invalid jscode","result":100240001}
            console.error(`requireConfig start appId=${appId},code=${code}`);
            xhr.open('GET', url, true);
            xhr.onreadystatechange = function () {
                console.error("xhr.requireOpenID", xhr);
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    let dat: { result: number, open_id: string, session_key: string, union_id: string } = null;
                    if (SDKTools.isAliPay) {
                        dat = xhr.responseText as any;
                    } else {
                        dat = JSON.parse(xhr.responseText);
                    }
                    WebDataProxy.inst.OpenIDData.fun设置OpenID(dat.open_id);
                    return resolve(true)
                } else {
                    return resolve(false)
                }
            };
            xhr.send();
        })
    }
}